package view.register;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import pojo.user.UserEntity;
import view.Main;
import controller.UserController;

/**
 * @author Limohan
 */
public class RegisterController {
    private Main myApp;

    public RegisterController() {
    }

    public void setApp(Main myApp){
        this.myApp = myApp;
    }

    @FXML
    private ToggleGroup group;

    @FXML
    private JFXTextField accountRegister;

    @FXML
    private JFXTextField passwordRegister;

    @FXML
    private JFXTextField nameRegister;

    @FXML
    private JFXTextField phoneRegister;

    @FXML
    private JFXTextField factoryNameRegister;

    @FXML
    private JFXTextField detailRegister;

    @FXML
    private JFXButton ConfirmRegister;

    @FXML
    private JFXButton ReturnWelcome;

    @FXML
    private JFXRadioButton yun;

    @FXML
    private JFXRadioButton jingXiao;

    @FXML
    private ImageView register;

    @FXML
    void initialize(){
        register.setImage(new Image("\\view\\admin\\image\\welcome.png",true));
    }

    public RegisterController(Main myApp) {
        this.myApp = myApp;
    }


    @FXML
    void confirmRegister(ActionEvent event) throws Exception {
        UserEntity userEntity = new UserEntity();
        UserController userController = UserController.getInstance();
        userEntity.setAccount(accountRegister.getText());
        userEntity.setPassword(passwordRegister.getText());
        userEntity.setName(nameRegister.getText());
        userEntity.setPhone(phoneRegister.getText());
        jingXiao.setUserData("经销商");
        yun.setUserData("云工厂");
        //判断power类型
        if(group.getSelectedToggle()==null) {
            userEntity.setPower(null);
        }else{
            userEntity.setPower(group.getSelectedToggle().getUserData().toString());
        }

        userEntity.setDetail(detailRegister.getText());
        userEntity.setFactoryName(factoryNameRegister.getText());
        userEntity.setState("关停");
        String flag = userController.register(userEntity);
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText(flag);
        alert.showAndWait();
    }

    @FXML
    void returnWelcome(ActionEvent event) {
        Stage stage = (Stage)ReturnWelcome.getScene().getWindow();
        stage.close();
    }


}
